<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pustaka_model extends CI_Model {

	function __construct()
	{
		parent::__construct();
		$this->CI = get_instance();
	}
	
	//==================================================================
	function get_buku(){
		$this->db->select('a.*,b.ID_SEKOLAH as ID_SEK, c.*, d.*,e.*,f.*,(select count(ID_EKSEMPLAR) from tb_perpus_eksemplar d where d.ID_BUKU=a.ID_BUKU) as jmleks');
		$this->db->from('tb_perpus_buku a');
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->join('tb_perpus_penulis c','c.ID_PENULIS=a.ID_PENULIS');
		$this->db->join('tb_perpus_katalog d','d.ID_KATALOG=a.ID_KATALOG');
		$this->db->join('tb_perpus_penerbit e','e.ID_PENERBIT=a.ID_PENERBIT');
		$this->db->join('tb_perpus_rak f','f.ID_RAK=d.ID_RAK');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->CI->flexigrid->build_query();
		$return['records'] = $this->db->get();
		
		$this->db->select('a.*,b.ID_SEKOLAH as ID_SEK, c.*, (select count(ID_EKSEMPLAR) from tb_perpus_eksemplar d where d.ID_BUKU=a.ID_BUKU) as jmleks');
		$this->db->from('tb_perpus_buku a');
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->join('tb_perpus_penulis c','c.ID_PENULIS=a.ID_PENULIS');
		$this->db->join('tb_perpus_katalog d','d.ID_KATALOG=a.ID_KATALOG');
		$this->db->join('tb_perpus_penerbit e','e.ID_PENERBIT=a.ID_PENERBIT');
		$this->db->join('tb_perpus_rak f','f.ID_RAK=d.ID_RAK');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->CI->flexigrid->build_query(FALSE);
		$return['record_count'] = $this->db->count_all_results();
		
		return $return;
	}
	function insert_buku($data){
		$query = $this->db->insert('tb_perpus_buku',$data);
		return $query;
	}
	function validasi_buku($kode){
		$this->db->select('count(*) as jml, ID_BUKU');
		$this->db->from('tb_perpus_buku');  
		$this->db->where('kode_buku',$kode);
		$query = $this->db->get();
		return $query;
	}
	function get_bukuID($kode){
		$this->db->select('a.*, b.ID_SEKOLAH as ID_SEK, c.*,d.*,e.*,f.*,g.*');
		$this->db->from('tb_perpus_buku a');  
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->join('tb_perpus_jenis_buku c','c.ID_JENIS=a.ID_JENIS');
		$this->db->join('tb_perpus_penulis d','d.ID_PENULIS=a.ID_PENULIS');
		$this->db->join('tb_perpus_katalog e','e.ID_KATALOG=a.ID_KATALOG');
		$this->db->join('tb_perpus_penerbit f','f.ID_PENERBIT=a.ID_PENERBIT');
		$this->db->join('tb_perpus_rak g','g.ID_RAK=e.ID_RAK');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('a.ID_BUKU',$kode);
		$query = $this->db->get();
		return $query;
	}
	
	function update_buku($data, $kode){
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('ID_BUKU',$kode);
		$this->db->update('tb_perpus_buku',$data);
	}
	
	function delete_buku($kode){
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('ID_BUKU',$kode);
		$this->db->delete('tb_perpus_buku'); 
	}
	
	function get_list_buku(){
		$this->db->select('*');
		$this->db->from('tb_perpus_buku');  
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$query = $this->db->get();
		return $query;
	}
	function getLastPustaka(){
		$this->db->select('max(id_buku) as maxid');
		$this->db->from('tb_perpus_buku');  
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$query = $this->db->get();
		return $query;
	}
	function insert_eksemplar($data){
		$this->db->insert('tb_perpus_eksemplar',$data);
	}
	
	function get_eksemplar($status=''){
		$this->db->select('a.*,b.ID_SEKOLAH as ID_SEK, c.*, d.*');
		$this->db->from('tb_perpus_eksemplar a');
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->join('tb_perpus_buku c','c.ID_BUKU=a.ID_BUKU');
		$this->db->join('tb_perpus_penulis d','d.ID_PENULIS=c.ID_PENULIS');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		if($status != '')
			$this->db->where('a.STATUS',1);
		$this->CI->flexigrid->build_query();
		$return['records'] = $this->db->get();
		
		$this->db->select('a.*,b.ID_SEKOLAH as ID_SEK, c.*, d.*');
		$this->db->from('tb_perpus_eksemplar a');
		$this->db->join('tb_master_sekolah b','b.ID_SEKOLAH=a.ID_SEKOLAH');
		$this->db->join('tb_perpus_buku c','c.ID_BUKU=a.ID_BUKU');
		$this->db->join('tb_perpus_penulis d','d.ID_PENULIS=c.ID_PENULIS');
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		if($status != '')
			$this->db->where('a.STATUS',1);
		$this->CI->flexigrid->build_query(FALSE);
		$return['record_count'] = $this->db->count_all_results();
		
		return $return;
	}
	
	function get_eksemplarID($idpustaka){
		$this->db->select('*');
		$this->db->from('tb_perpus_eksemplar');
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('ID_BUKU',$idpustaka);
		$query = $this->db->get();
		return $query;
	}
	function delete_eksemplar($kode){
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('ID_EKSEMPLAR',$kode);
		$this->db->delete('tb_perpus_eksemplar'); 
	}
	
	function get_konfigurasi($jns_anggota){
		$this->db->select('*');
		$this->db->from('tb_perpus_konfigurasi');
		$this->db->where('JENIS_ANGGOTA',$jns_anggota);
		$query = $this->db->get();
		return $query;
	}
	
	function update_eksemplar($data, $kode_eks){
		$this->db->where('KODE_EKSEMPLAR',$kode_eks);
		$this->db->update('tb_perpus_eksemplar',$data);
	}
}

?>
